<?php
namespace app\common\validate;
use think\Validate;
use think\Db;
class CommonV extends Validate{
    protected $rule = [
    	'nickName'		=> ['require'],
    	'gender'		=> ['require'],
    	'city'			=> ['require'],
    	'province'		=> ['require'],
    	'avatarUrl'		=> ['require'],
    	'code'			=> ['require'],
    	'phone'			=> ['require','checkphone'],
    	'smscode'		=> ['require','checkSmscode:sms'],
    ];
	protected $message=[
		'nickName.require'	=>'昵称不能为空',
		'gender.require'	=>'性别不能为空',
		'city.require'		=>'城市名不能为空',
		'province.require'	=>'省份名不能为空',
		'avatarUrl.require'	=>'头像地址不能为空',
		'code.require'		=>'临时授权码不能为空',
		'phone.require'	    => '手机号码不能为空',
		'phone.checkphone'  => '手机号码格式错误',
		'smscode.require'		=> '手机验证码不能为空',
		'smscode.checkSmscode'	=> '验证码错误或已过期',
	];
	protected $scene=[
		'register'=>[		//手机号注册用户必备参数
			'nickName',
			'gender',
			'city',
			'province',
			'avatarUrl',
			'phone',
			'smscode'
		],
		'update_user'=>[		//手机号注册用户必备参数
			'nickName',
			'city',
			'province',
			'avatarUrl'
		],
		'login'=>['code'],
		'login2'=>[
			'smscode',
			'phone'
		],
	];
	//验证手机号
	protected function checkphone($val){
		return check_mobile($val);
	}
	//验证验证码
	protected function checkSmscode($key,$rule,$data){
		$map=[
			'phone' 	=> $data['phone'],
			'stat'  	=> 1
		];
		$smsLog = Db::name('smsLog')->where($map)->order('id desc')->find();
		if(!$smsLog){
			return false;
		}else{
			Db::name('smsLog')->where('id',$smsLog['id'])->update(['stat' => 2]);
			if($smsLog['smscode'] != $data['smscode']){
				return false;
			}elseif($smsLog['exp_time'] < time()){
				return false;
			}else{
				return true;
			}	
		}
	}
//	protected function checkIdcard($val){
//		return isIdCard($val);
//	}
}
